//
//  ViewController.m
//  Rac
//
//  Created by ios on 2017/3/1.
//  Copyright © 2017年 Wy. All rights reserved.
//

#import "ViewController.h"
#import "TestModel.h"
#import "HeadView.h"
#import "TestViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property(nonatomic,strong)TestModel *viewModel;
@property(nonatomic,strong)HeadView *headView;

@end

@implementation ViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    self.viewModel = [TestModel new];
    
    [self.view addSubview:self.headView];
    
    @weakify(self);
    
    [self.headView.btnSingle subscribeNext:^(id x) {
         @strongify(self)
        self.label.text = [NSString stringWithFormat:@"%@",x];
        
    }];
    
    
    [self.viewModel.refreshCommand execute:self.headView];
    
}

-(void)fffff{
    //    RAC(self.textField,text) = [RACObserve(self.viewModel, name) distinctUntilChanged];
    //    RAC(self.label,text)     = [RACObserve(self.textField, text) distinctUntilChanged];
    //    @weakify(self);
    
    //    [[self.textField.rac_textSignal distinctUntilChanged] subscribeNext:^(NSString * _Nullable x) {
    //     @strongify(self);
    //
    //     self.viewModel.name = x;
    //
    //    } error:^(NSError * _Nullable error) {
    //
    //
    //    } completed:^{
    //
    //
    //    }];
    
    //    RAC(self.btn,enabled) = [RACSignal combineLatest:@[
    //                                                       self.textField.rac_textSignal,
    //
    //                                                       ]
    //                                              reduce:^id _Nullable{
    //                                                  return @(self.viewModel.name.length >5);
    //                                              }];
    //
    //    [self.btn addTarget:self
    //                          action:@selector(uploadData:)
    //                forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)uploadData:(UIButton *)sender{
    
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnClick:(id)sender {
    
    [self.viewModel.btnCommand execute:self.textField];
    
}

-(HeadView *)headView{
    
    if (!_headView) {
        _headView = [[HeadView alloc] initWithFrame:CGRectMake(100, 100, 80, 80)];
        
    }
    return _headView;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    TestViewController *vc =[[TestViewController alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}


@end
